#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
// #include "protocol_examples_common.h"
#include "esp_mac.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "sdkconfig.h"
#include "mbedtls/md5.h"
#include <cJSON.h>
#include "esp_http_client.h"
// #include "string.h"
// #include "cstring"
#include "stdlib.h"
#include "bind_utils.h"
#include<iostream>
using namespace std;
static const char *TAG = "bind_utils";

char* md5_encode(const char *input,char output_str[33]){
    // 初始化 MD5 上下文
    mbedtls_md5_context ctx;
    mbedtls_md5_init(&ctx);
    ESP_LOGI(TAG, "MD5 加密前: [%s]", input);
    // 输入数据
    uint8_t output[16]; // MD5 输出结果为 16 字节
    // char output_str[33]; // 用于存储 32 位小写的十六进制字符串

    // 执行 MD5 加密
    mbedtls_md5_starts(&ctx);
    mbedtls_md5_update(&ctx, (const unsigned char *)input, strlen(input));
    mbedtls_md5_finish(&ctx, output);

    // 将结果转换为 32 位小写的十六进制字符串
    for (int i = 0; i < 16; i++) {
        sprintf(&output_str[i * 2], "%02x", output[i]);
    }

    // 输出结果
    ESP_LOGI(TAG, "MD5 加密前: [%s]", input);
    ESP_LOGI(TAG, "MD5 加密后 (32 位小写): %s", output_str);

    // 释放 MD5 上下文
    mbedtls_md5_free(&ctx);
    return output_str;
}
char * get_mac_address(char mac[13]){
    uint8_t temp[6] = {0};
    esp_err_t ret = esp_read_mac(temp, ESP_MAC_WIFI_STA);
    if (ret == ESP_OK) {
        ESP_LOGI("MAC Address", "MAC: %02x:%02x:%02x:%02x:%02x:%02x",
                 temp[0], temp[1], temp[2], temp[3], temp[4], temp[5]);
    } else {
        ESP_LOGE("MAC Address", "Failed to read MAC address");
    }
    mac[6] = '\0';
    return mac;
}
nvs_handle_t my_handle;
void set_token_by_nvs(const char *token){
    esp_err_t err = nvs_open("storage", NVS_READWRITE, &my_handle);
    if (err != ESP_OK) {
        ESP_LOGE("NVS", "Error (%s) opening NVS handle!", esp_err_to_name(err));
        return;
    }
    //存字符串
    err = nvs_set_str(my_handle, "token", token);
    if (err != ESP_OK) {
        ESP_LOGE("NVS", "Error (%s) writing to NVS!", esp_err_to_name(err));
    }
    //提交更改
    err = nvs_commit(my_handle);
    if (err != ESP_OK) {
        ESP_LOGE("NVS", "Error (%s) committing to NVS!", esp_err_to_name(err));
    }
    nvs_close(my_handle);
}
void get_token_by_nvs(char buf[]){
     esp_err_t err = nvs_open("storage", NVS_READWRITE, &my_handle);
    if (err != ESP_OK) {
        ESP_LOGE("NVS", "Error (%s) opening NVS handle!", esp_err_to_name(err));
        return;
    }
     //读取字符串
    size_t required_size;
    err = nvs_get_str(my_handle, "token", NULL, &required_size);
    if (err != ESP_OK) {
        ESP_LOGE("NVS", "Error (%s) reading from NVS!", esp_err_to_name(err));
        return;
    }
    char *token = (char*)malloc(required_size);
    // char *token;

    if (token == NULL) {
        ESP_LOGE("NVS", "Memory allocation failed");
        return ;
    }
    err = nvs_get_str(my_handle, "token", token, &required_size);
    if (err == ESP_OK) {
        ESP_LOGI("NVS", "token = %s", token);
    } else {
        ESP_LOGE("NVS", "Error (%s) reading from NVS!", esp_err_to_name(err));
    }
    uint8_t len_token = strlen(token);
    memcpy(buf,token,len_token);
    buf[len_token] = '\0';
    nvs_close(my_handle);
    free(token);
    return;
}
